package com.hctforgreen.greenservice.examine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.ParentActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.ExamPaperEntity;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;

/**
 * 考试信息展示页面
 *
 * @author Tallen Zhai
 *
 */
public class PaperActivity extends ParentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paper_activity);
        initSkinLayout();
        initFinshExamineActivityReceiver();
        initTitleButtonBar();
        Intent intent = getIntent();
        ExamPaperEntity paperEntity = (ExamPaperEntity) intent
                .getSerializableExtra(HctConstants.ON_ACTIVITY_KEY_PAPER_ENTITY);
        if (paperEntity != null) {
            initWindow(paperEntity);
            initStartBtn(paperEntity);
        } else {
            final String paperId = getIntent().getStringExtra(
                    HctConstants.ON_ACTIVITY_KEY_PAPER_ID);
            initPaperById(paperId);
        }
    }

    /**
     * 通过试卷id后去试卷详情
     */
    private void initPaperById(final String paperId) {
        @SuppressLint("HandlerLeak") final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HctConstants.STATUS_SUCCESS:
                        HctResult result = (HctResult) msg.obj;
                        if (result.data != null) {
                            final ExamPaperEntity entity = (ExamPaperEntity) result.data;
                            initWindow(entity);
                            initStartBtn(entity);
                        } else {
                            Toast.makeText(PaperActivity.this,
                                    getString(R.string.empty), Toast.LENGTH_LONG)
                                    .show();
                        }

                        break;
                    case HctConstants.STATUS_FAILD:
                        Toast.makeText(PaperActivity.this,
                                getString(R.string.net_error_hint),
                                Toast.LENGTH_LONG).show();
                        break;
                }
                super.handleMessage(msg);
            }
        };
        new HandlerThread("getPapperById") {
            public void run() {
                Message message = handler.obtainMessage();
                HctController controller = new HctController(PaperActivity.this);
                try {
                    HctResult hctResult = controller.getExamPageByID(paperId);
                    message.what = HctConstants.STATUS_SUCCESS;
                    message.obj = hctResult;
                } catch (Exception e) {
                    message.what = HctConstants.STATUS_FAILD;
                    e.printStackTrace();
                }
                handler.sendMessage(message);
            }
        }.start();

    }

    private void initTitleButtonBar() {
        final TextView titleTv = (TextView) findViewById(R.id.tv_title);
        titleTv.setText(getString(R.string.train_examine));

        final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
        backBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // InitTitle.initTitle(this, R.drawable.btn_reback, "考试", 0,
        // new OnClickListener() {
        // @Override
        // public void onClick(View v) {
        // finish();
        // }
        //
        // }, null);
    }

    private void initWindow(final ExamPaperEntity paperEntity) {
        // final TextView userNameTv = (TextView)
        // findViewById(R.id.tv_user_name);
        final TextView titleTv = (TextView) findViewById(R.id.paper_title);
        final TextView introTv = (TextView) findViewById(R.id.paper_intro);// 简介||备注
        final TextView timesTv = (TextView) findViewById(R.id.tv_time);// 发布时间
        final TextView totalPointTv = (TextView) findViewById(R.id.tv_score);// 总分
        final TextView passScore = (TextView) findViewById(R.id.tv_qualified_score);// 及格分
        // userNameTv
        // .setText(infoStoreUtil.getUserInfo(PaperActivity.this).userName);
        titleTv.setText(paperEntity.name);
        introTv.setText(paperEntity.remark);
        timesTv.setText(paperEntity.createTime);
        totalPointTv.setText(paperEntity.score + "");
        passScore.setText(paperEntity.passScore + "");
    }

    private void initStartBtn(final ExamPaperEntity paperEntity) {
        Button startBtn = (Button) findViewById(R.id.btn_start);
        startBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(PaperActivity.this,
                        CountDownExamDetailActivity.class);
                intent.putExtra(HctConstants.ON_ACTIVITY_KEY_PAPER_ENTITY,
                        paperEntity);
                startActivity(intent);
            }
        });
    }
}
